// src/index.ts

import {DataSource, DataSourceOptions} from "typeorm"
import {Post} from "./entities/Post"

const options: DataSourceOptions =  {
    type: 'mongodb',
    host: '95.169.25.181',
    port: 12300,
    database: 'test',
    entities: [__dirname + '/entities/*.ts'],
    synchronize: true,
    useNewUrlParser: true,
    logging: true,
}

const dataSource = new DataSource(options)
dataSource.initialize().then(
    async (dataSource) => {
        let post = new Post()
        post.text = "Hello how are you?"
        post.title = "hello"
        post.likesCount = 100

        let postRepository = dataSource.getRepository(Post)

        postRepository
            .save(post)
            .then((post) => console.log("Post has been saved: ", post))
            .catch((error) => console.log("Cannot save. Error: ", error))
    },
    (error) => console.log("Cannot connect: ", error),
)
